<template>
    <div class="container">
        <el-form :model="ruleForm" status-icon :rules="rules2" ref="ruleForm" label-width="100px"
                 class="demo-ruleForm">
            <el-form-item label="名称" prop="p1">
                <el-input type="text" v-model="ruleForm.p1" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="p2">
                <el-input type="number" v-model="ruleForm.p2" auto-complete="off"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import pub from '../../utils/pub'

    export default {
        data: function () {
            var v1 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入(2-20字)'));
                } else if (value < 2 || value.length > 10) {
                    callback(new Error('2-20位'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    p1: '',
                    p2: '',
                },
                rules2: {
                    p1: [
                        {required: true, message: '请输入名称', trigger: 'blur'}
                    ],
                    p2: [
                        {required: true, validator: v1, trigger: 'blur'}
                    ],
                }
            }
        },
        components: {
        },
        props: ['uid'],
        methods: {
            loadopen() {
                this.loading = this.$loading({lock: true,text: 'Loading',spinner: 'el-icon-loading',background: 'rgba(0, 0, 0, 0.2)'});
                setTimeout(() => {this.loading.close();}, 5000);
            },
            loadclose() {this.loading.close();},
            // 获取角色信息
            getInfo(row) {
                // console.log(this.uid)
                if(!this.uid) {
                    this.ruleForm.p1 = '';
                    this.ruleForm.p2 = '';
                    return;
                }
                this.loadopen();
                this.config.get('/admin/findById', {id: this.uid}).then(res => {
                    this.loadclose();
                    if (res.data.code === '0') {
                        // console.log(res.data, '获取详情信息');
                        this.ruleForm.p1 = res.data.name;
                        this.ruleForm.p2 = res.data.sort;
                    } else {
                        this.$message.error(this.config.handleError(res.data));
                    }
                });
            },
            // 验证和提交表单
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let obj = {
                            'admin.id': this.uid,
                            'admin.name': this.ruleForm.p1,
                            'admin.describe': this.ruleForm.p2,
                        };
                        let url = this.uid ? '/admin/update' : '/admin/add';
                        this.loadopen();
                        this.config.post(url, obj).then(res => {
                            this.loadclose();
                            if (res.data.code === '0') {
                                this.$message({message: res.data.message,type: 'success'});
                            } else {
                                this.$message.error(this.config.handleError(res.data));
                            }
                            this.$emit('done', '');
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
        },
        mounted() {
        }
    }
</script>
